//
//  WmcTeamWatermarkCategoriesVM.swift
//  YPWatermarkCamera
//
//  Created by yupao_ios_macmini06 on 2022/9/23.
//

import Foundation
import RxRelay

class WmcTeamWatermarkCategoriesVM: WmcBaseVM {
    var teamAblum: WmcMediaTeamAlbumModel?
    let watermarks: BehaviorRelay<[WmcMediaPhotographStatisticsModel]> = .init(value: [])
    let error: BehaviorRelay<Error?> = .init(value: nil)

    /// 获取成员列表
    /// - Returns: description
    func getWatermarks() {
        guard let busId = teamAblum?.busId else {
            return
        }
        WmcHudManager.loading()
        WmcMediaTeamAlbumAPI.fetchTakePhotoStatistics(busId: Int(busId) ?? 0).subscribe { [weak self] list in
            guard let weakSelf = self else { return }
            weakSelf.watermarks.accept(list)
            WmcHudManager.dismiss()
        } onError: { [weak self] error in
            guard let weakSelf = self else { return }
            WmcHudManager.toast(error)
            WmcHudManager.dismiss()
            weakSelf.error.accept(error)
        }.disposed(by: rx.disposeBag)
    }
    
    func getWatermarks(success: ((_ list: [WmcMediaPhotographStatisticsModel]) -> Void)?,failure: ((_ error: Error) -> Void)?) -> Void {
        guard let busId = teamAblum?.busId else {
            return
        }
        WmcHudManager.loading()
        WmcMediaTeamAlbumAPI.fetchTakePhotoStatistics(busId: Int(busId) ?? 0).subscribe { [weak self] list in
            guard let weakSelf = self else { return }
            weakSelf.watermarks.accept(list)
            WmcHudManager.dismiss()
            guard let success = success else { return }
            success(list)
        } onError: { [weak self] error in
            guard let weakSelf = self else { return }
            WmcHudManager.toast(error)
            WmcHudManager.dismiss()
            weakSelf.error.accept(error)
            guard let failure = failure else { return }
            failure(error)
        }.disposed(by: rx.disposeBag)
    }
}
